package com.ctshk.rpc.booking.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.dto.system.admission.info.SupplierSystemOrderAdmissionTicketDetailInfoDTO;
import com.ctshk.rpc.booking.dto.system.admission.info.SupplierSystemOrderAdmissionTicketInfoDTO;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderAdmissionTicket;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderAdmissionTicketDetail;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderAdmissionTicketDetailMapper;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderAdmissionTicketMapper;
import com.ctshk.rpc.booking.req.system.admission.add.SupplierSystemOrderAdmissionTicketAddReq;
import com.ctshk.rpc.booking.req.system.admission.edit.SupplierSystemOrderAdmissionEditReq;
import com.ctshk.rpc.booking.service.ISupplierSystemOrderAdmissionTicketService;
import com.ctshk.rpc.order.scenic.dto.ProductTicketDTO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统内门票基础信息 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-25
 */
@DubboService
public class SupplierSystemOrderAdmissionTicketServiceImpl implements ISupplierSystemOrderAdmissionTicketService {
    @Autowired
    SupplierSystemOrderAdmissionTicketMapper supplierSystemOrderAdmissionTicketMapper;

    @Autowired
    SupplierSystemOrderAdmissionTicketDetailMapper supplierSystemOrderAdmissionTicketDetailMapper;

    @Override
    public void add(Long supplierOrderId, Long userId, LocalDateTime nowDateTime, List<SupplierSystemOrderAdmissionTicketAddReq> supplierSystemOrderAdmissionAddReqList) {
        if (supplierOrderId != null) {
            //删除
            QueryWrapper<SupplierSystemOrderAdmissionTicket> systemOrderAdmissionWrapper = new QueryWrapper<>();
            systemOrderAdmissionWrapper.lambda().eq(SupplierSystemOrderAdmissionTicket::getOrderId, supplierOrderId);
            supplierSystemOrderAdmissionTicketMapper.delete(systemOrderAdmissionWrapper);
            QueryWrapper<SupplierSystemOrderAdmissionTicketDetail> systemOrderAdmissionTicketWrapper = new QueryWrapper<>();
            systemOrderAdmissionTicketWrapper.lambda().eq(SupplierSystemOrderAdmissionTicketDetail::getOrderId, supplierOrderId);
            supplierSystemOrderAdmissionTicketDetailMapper.delete(systemOrderAdmissionTicketWrapper);
        }
        //新增门票信息
        if (CollectionUtils.isNotEmpty(supplierSystemOrderAdmissionAddReqList)) {
            supplierSystemOrderAdmissionAddReqList.forEach(systemOrderAdmissionReq -> {
                SupplierSystemOrderAdmissionTicket supplierSystemOrderAdmission = EntityUtil.copy(systemOrderAdmissionReq, SupplierSystemOrderAdmissionTicket.class);
                supplierSystemOrderAdmission.setId(SnowflakeIdWorker.nextId());
                supplierSystemOrderAdmission.setOrderId(supplierOrderId);
                supplierSystemOrderAdmission.setCreateId(userId);
                supplierSystemOrderAdmission.setGmtCreate(nowDateTime);
                supplierSystemOrderAdmission.setModifiedId(userId);
                supplierSystemOrderAdmission.setGmtModified(nowDateTime);
                supplierSystemOrderAdmission.setIsDeleted(IsDeletedCode.NO.getCode());
                supplierSystemOrderAdmissionTicketMapper.insert(supplierSystemOrderAdmission);
                if (CollectionUtils.isNotEmpty(systemOrderAdmissionReq.getSupplierSystemOrderAdmissionTicketDetailAddReqList())) {
                    //新增旅客类型信息
                    systemOrderAdmissionReq.getSupplierSystemOrderAdmissionTicketDetailAddReqList().forEach(systemOrderAdmissionTicketDetailReq -> {
                        SupplierSystemOrderAdmissionTicketDetail supplierSystemOrderAdmissionTicketDetail = EntityUtil.copy(systemOrderAdmissionTicketDetailReq, SupplierSystemOrderAdmissionTicketDetail.class);
                        supplierSystemOrderAdmissionTicketDetail.setId(SnowflakeIdWorker.nextId());
                        supplierSystemOrderAdmissionTicketDetail.setOrderId(supplierOrderId);
                        supplierSystemOrderAdmissionTicketDetail.setAirExpressInfoId(supplierSystemOrderAdmission.getId());
                        supplierSystemOrderAdmissionTicketDetail.setCreateId(userId);
                        supplierSystemOrderAdmissionTicketDetail.setGmtCreate(nowDateTime);
                        supplierSystemOrderAdmissionTicketDetail.setModifiedId(userId);
                        supplierSystemOrderAdmissionTicketDetail.setGmtModified(nowDateTime);
                        supplierSystemOrderAdmissionTicketDetail.setIsDeleted(IsDeletedCode.NO.getCode());
                        supplierSystemOrderAdmissionTicketDetailMapper.insert(supplierSystemOrderAdmissionTicketDetail);
                    });
                }
            });
        }
    }

    @Override
    public void update(Long userId, LocalDateTime nowDateTime, List<SupplierSystemOrderAdmissionEditReq> supplierSystemOrderAdmissionEditReqList) {
        if (CollectionUtils.isNotEmpty(supplierSystemOrderAdmissionEditReqList)) {
            for (SupplierSystemOrderAdmissionEditReq supplierSystemOrderAdmissionEditReq : supplierSystemOrderAdmissionEditReqList) {
                UpdateWrapper<SupplierSystemOrderAdmissionTicket> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(SupplierSystemOrderAdmissionTicket::getId, supplierSystemOrderAdmissionEditReq.getId());
                updateWrapper.lambda().set(SupplierSystemOrderAdmissionTicket::getCustomerRemark, supplierSystemOrderAdmissionEditReq.getCustomerRemark());
                updateWrapper.lambda().set(SupplierSystemOrderAdmissionTicket::getSupplierRemark, supplierSystemOrderAdmissionEditReq.getSupplierRemark());
                updateWrapper.lambda().set(SupplierSystemOrderAdmissionTicket::getModifiedId, userId);
                updateWrapper.lambda().set(SupplierSystemOrderAdmissionTicket::getGmtModified, nowDateTime);
                supplierSystemOrderAdmissionTicketMapper.update(new SupplierSystemOrderAdmissionTicket(), updateWrapper);
                supplierSystemOrderAdmissionEditReq.getSupplierSystemOrderAdmissionTicketDetailEditReqList().forEach(admissionTicketEditReq -> {
                    UpdateWrapper<SupplierSystemOrderAdmissionTicketDetail> ticketUpdateWrapper = new UpdateWrapper<>();
                    ticketUpdateWrapper.lambda().eq(SupplierSystemOrderAdmissionTicketDetail::getId, admissionTicketEditReq.getId());
                    ticketUpdateWrapper.lambda().set(SupplierSystemOrderAdmissionTicketDetail::getUnitPrice, admissionTicketEditReq.getUnitPrice());
                    ticketUpdateWrapper.lambda().set(SupplierSystemOrderAdmissionTicketDetail::getModifiedId, userId);
                    ticketUpdateWrapper.lambda().set(SupplierSystemOrderAdmissionTicketDetail::getGmtModified, nowDateTime);
                    supplierSystemOrderAdmissionTicketDetailMapper.update(new SupplierSystemOrderAdmissionTicketDetail(), ticketUpdateWrapper);
                });
            }
        }
    }

    @Override
    public List<SupplierSystemOrderAdmissionTicketInfoDTO> selectByOrderId(Long id) {
        List<SupplierSystemOrderAdmissionTicketInfoDTO> supplierSystemOrderAdmissionTicketInfoDTOList = new ArrayList<>();
        //查询机票列表
        QueryWrapper<SupplierSystemOrderAdmissionTicket> admissionWrapper = new QueryWrapper();
        admissionWrapper.lambda().eq(SupplierSystemOrderAdmissionTicket::getOrderId, id);
        List<SupplierSystemOrderAdmissionTicket> supplierSystemOrderAdmissions = supplierSystemOrderAdmissionTicketMapper.selectList(admissionWrapper);
        if (CollectionUtils.isNotEmpty(supplierSystemOrderAdmissions)) {
            supplierSystemOrderAdmissionTicketInfoDTOList = EntityUtil.copyList(supplierSystemOrderAdmissions, SupplierSystemOrderAdmissionTicketInfoDTO.class);
            supplierSystemOrderAdmissionTicketInfoDTOList.forEach(admission -> {
                //查询机票旅客类型列表
                QueryWrapper<SupplierSystemOrderAdmissionTicketDetail> AdmissionTicketDetailWrapper = new QueryWrapper();
                AdmissionTicketDetailWrapper.lambda().eq(SupplierSystemOrderAdmissionTicketDetail::getOrderId, id);
                AdmissionTicketDetailWrapper.lambda().eq(SupplierSystemOrderAdmissionTicketDetail::getAirExpressInfoId, admission.getId());
                List<SupplierSystemOrderAdmissionTicketDetail> supplierSystemOrderAdmissionTicketDetails = supplierSystemOrderAdmissionTicketDetailMapper.selectList(AdmissionTicketDetailWrapper);
                if (CollectionUtils.isNotEmpty(supplierSystemOrderAdmissionTicketDetails)) {
                    List<SupplierSystemOrderAdmissionTicketDetailInfoDTO> supplierSystemOrderAdmissionTicketDetailInfoDTOList = EntityUtil.copyList(supplierSystemOrderAdmissionTicketDetails, SupplierSystemOrderAdmissionTicketDetailInfoDTO.class);
                    admission.setSupplierSystemOrderAdmissionTicketDetailInfoDTOList(supplierSystemOrderAdmissionTicketDetailInfoDTOList);
                }
            });
        }
        return supplierSystemOrderAdmissionTicketInfoDTOList;
    }

    @Override
    public String getSystemAdmissionTickets(Long orderId) {
        SupplierSystemOrderAdmissionTicket supplierSystemOrderAdmissionTicket = supplierSystemOrderAdmissionTicketMapper.selectOne(new QueryWrapper<SupplierSystemOrderAdmissionTicket>().lambda()
                .eq(SupplierSystemOrderAdmissionTicket::getOrderId, orderId)
                .eq(SupplierSystemOrderAdmissionTicket::getIsDeleted, IsDeletedCode.NO.getCode())
                .select(SupplierSystemOrderAdmissionTicket::getTicketNumber));
        // 验证格式是否正确
        List<ProductTicketDTO> productTicketDTOS = JSON.parseArray(supplierSystemOrderAdmissionTicket.getTicketNumber(), ProductTicketDTO.class);
        return supplierSystemOrderAdmissionTicket.getTicketNumber();
    }
}
